﻿using AutoMapper;
using DFGK_WMS.Context;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK_WMS.Repository
{
    public class PickingPositionRepository : BaseRepository<PickingPositionModel>, IPickingPositionRepository
    {
        public PickingPositionRepository(DFGK_DB context, IMapper mapper) : base(context, mapper)
        {
        }

        /// <summary>
        /// 添加拣选位
        /// </summary>
        /// <param name="pickingPosition"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool AddPickingPositon(PickingPositionModel pickingPosition,out string msg)
        {
            msg = "";
            var PickingPositionList = Context.PickingPositionModel.AsNoTracking().FirstOrDefault(x => x.PickingPositionCode == pickingPosition.PickingPositionCode);
            if(PickingPositionList!=null)
            {
                msg = "库中已存在该拣选位";
                return false;
            }
            Context.PickingPositionModel.Add(pickingPosition);
            Context.SaveChanges();
            var BindPickingPositionList = Context.BindPickingPositionModel.AsNoTracking().FirstOrDefault(x => x.PickingPositionId == pickingPosition.Id);
            if (BindPickingPositionList != null)
            {
                msg = "库中已存在该拣选位";
                return false;
            }
            BindPickingPositionModel bindPickingPositionModel = new BindPickingPositionModel();
            bindPickingPositionModel.PickingPositionId = pickingPosition.Id;
            bindPickingPositionModel.PickingPositonName = pickingPosition.PickingPositionName;
            bindPickingPositionModel.PickingPositionCode = pickingPosition.PickingPositionCode;
            Context.BindPickingPositionModel.Add(bindPickingPositionModel);

            if (Context.SaveChanges() > 0)
            {            
                return true;
            }
            var pobj = Context.PickingPositionModel.Find(pickingPosition.Id);
            if(pobj!=null)
            Context.PickingPositionModel.Remove(pobj);
            Context.SaveChanges();
            msg = "添加失败";
            return false;
        }

        /// <summary>
        /// 删除拣选位 
        /// </summary>
        /// <param name="pickingPositionId"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool DelPickingPosition(string pickingPositionCode, out string msg)
        {
            msg = "";
            var PickingPostionobj = Context.PickingPositionModel.AsNoTracking().FirstOrDefault(x => x.PickingPositionCode == pickingPositionCode);
            if (PickingPostionobj != null)
            {
                Context.PickingPositionModel.Remove(PickingPostionobj);
                var BinPickobj = Context.BindPickingPositionModel.AsNoTracking().FirstOrDefault(x => x.PickingPositionId == PickingPostionobj.Id);
                if(BinPickobj!=null)
                Context.BindPickingPositionModel.Remove(BinPickobj);
            }
            if (Context.SaveChanges() > 0)
            {
                return true;
            }
            msg = "删除失败";
            return false;
        }

        /// <summary>
        /// 获取所有拣选位
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<PickingPositionModel> GetAllPickingPositions()
        {
            return Context.PickingPositionModel.AsQueryable().ToList();
        }

        /// <summary>
        /// PDA拣选位置
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<BindPickingPositionModel> GetBindPickingPosition()
        {
            var blist = Context.BindPickingPositionModel.AsNoTracking().Where(x => x.Status == 0);

            return blist.ToList();
        }

        /// <summary>
        /// 根据ID获取指定拣选位
        /// </summary>
        /// <param name="pickingPositionId"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public PickingPositionModel GetPickingPositionById(string pickingPositionCode)
        {
            var list = Context.PickingPositionModel.Where(x => x.PickingPositionCode == pickingPositionCode).ToList();
            if(list.Count <= 0)
            {
                return null;
            }
            var pickingPosition = list.First();
            return pickingPosition;
        }

        /// <summary>
        /// 退出登录
        /// </summary>
        /// <param name="stationcode"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool LoginOut(string stationcode)
        {
            var binobj = Context.BindPickingPositionModel.FirstOrDefault(x => x.PickingPositionCode == stationcode);
            if(binobj!=null)
            {
               binobj.Status = 0;
            }
            return Context.SaveChanges()>0;
        }

        public bool ResetPicking(string stationcode)
        {
            var binobj = Context.BindPickingPositionModel.FirstOrDefault(x => x.PickingPositionCode == stationcode);
            if (binobj != null)
            {
                binobj.Status = 0;
            }
            return Context.SaveChanges() > 0;
        }

        /// <summary>
        /// 更新拣选位
        /// </summary>
        /// <param name="pickingPosition"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool UpdPickingPosition(PickingPositionModel pickingPosition, out string msg)
        {
            msg = "";
            var list = Context.PickingPositionModel.Where(x => x.PickingPositionId == pickingPosition.PickingPositionId).ToList();
            if(list.Count <= 0)
            {
                msg = "未查询到该拣选位";
                return false;
            }
            var listFirst = list.First();
            listFirst.PickingPositionName = pickingPosition.PickingPositionName;
            listFirst.PickingPositionCode = pickingPosition.PickingPositionCode;
            listFirst.PickingPositionId = pickingPosition.PickingPositionId;
            Context.PickingPositionModel.Update(listFirst);
            if (Context.SaveChanges() > 0)
            {
                return true;
            }
            msg = "修改失败";
            return false;
        }
    }
}
